const LinkUrl = require('../common/plublic.js');
var app = getApp();
var PostData = function (options, callback, opeType, isdata) {
    var that = this
    wx.request({
      url: LinkUrl.URLLINK,
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      data: { 
        "jsonParam" : json2Form(options.data, opeType, isdata),
        "requestFrom": "WeChat"
        },
      //研发事业部2017-10-10 结束

        success: function (res) {
            if (res == null || res.data == null) {
                console.log('网络请求失败');
                return;
            }
            callback(res)
        },
        fail:function(){
          wx.hideLoading()
          wx.showToast({
            title: '请求超时',
            icon:'none'
          })
        }
    })
}

function json2Form(json, opeType, isdata) {

    var str = [];
    for (var p in json) {
        str.push('"' + encodeURIComponent(p) + '"' + ":" + '"' + encodeURIComponent(json[p]) + '"');
    }
    var jsondata = '';
    var thereturn = '"opeType":"' + opeType + '"';

    if (isdata == '1') {
        jsondata = ',"map":{' + str.join(",") + "}";
        thereturn = '{' + thereturn + jsondata + '}';
    } else if (isdata == '2') {
        jsondata = ',"map":' + JSON.stringify(json);
        thereturn = '{' + thereturn + jsondata + '}';
    } else {
        thereturn = '{' + thereturn + '}';
    }
    return thereturn;
}

module.exports = PostData;